home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / contrib / campbell / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  1.5 KB  |  63 lines

  1. /*
  2.  * getopt - get option letter from argv
  3.  *      This software is in the public domain
  4.  *      Originally written by Henry Spencer at the U. of Toronto
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. char    *optarg;        /* Global argument pointer. */
  10. int     optind = 0;     /* Global argv index. */
  11.  
  12. static char     *scan = NULL;   /* Private scan pointer. */
  13.  
  14. /* extern char     *index();  obsolete, used strchr (JDC). */
  15.  
  16. int
  17. getopt(argc, argv, optstring)
  18. int argc;
  19. char *argv[];
  20. char *optstring;
  21. {
  22.         register char c;
  23.         register char *place;
  24.  
  25.         optarg = NULL;
  26.  
  27.         if (scan == NULL || *scan == '\0') {
  28.                 if (optind == 0)
  29.                         optind++;
  30.  
  31.                 if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
  32.                         return(EOF);
  33.                 if (strcmp(argv[optind], "--")==0) {
  34.                         optind++;
  35.                         return(EOF);
  36.                 }
  37.  
  38.                 scan = argv[optind]+1;
  39.                 optind++;
  40.         }
  41.  
  42.         c = *scan++;
  43.         place = strchr(optstring, c);
  44.  
  45.         if (place == NULL || c == ':') {
  46.                 fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
  47.                 return('?');
  48.         }
  49.  
  50.         place++;
  51.         if (*place == ':') {
  52.                 if (*scan != '\0') {
  53.                         optarg = scan;
  54.                         scan = NULL;
  55.                 } else {
  56.                         optarg = argv[optind];
  57.                         optind++;
  58.                 }
  59.         }
  60.  
  61.         return(c);
  62. }
  63.